package com.photoeditor.demo.ui.adapter.share;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;

import com.photoeditor.demo.ui.widget.share.ShareImageItem;
import com.photoeditor.demo.ui.widget.share.ShareImageItem.ShareImageItemData;
import com.photoeditor.demo.ui.widget.share.ShareImageItem.ViewHolder;
import com.photoeditor.demo.ui.widget.share.ShareImageLittleItem;

import java.util.List;

public class ShareImageLittleListAdapter extends ArrayAdapter<ShareImageItemData> {

    private Context mContext;

    private BroadcastReceiver mInstalledReceiver;

    private List<ShareImageItemData> mData;

    public ShareImageLittleListAdapter(Context context, List<ShareImageItemData> data) {
        super(context, 0, data);
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public ShareImageItemData getItem(int position) {
        // TODO Auto-generated method stub
        if (position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ShareImageItem.ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = new ShareImageLittleItem(mContext);
            holder.icon = ((ShareImageLittleItem) convertView).getmIcon();
            holder.label = ((ShareImageLittleItem) convertView).getmLabel();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ShareImageItemData data = mData.get(position);
        View v = convertView.findViewById(android.R.id.button1);
        if (v != null) {
            ((RelativeLayout) convertView).removeView(v);
        }
        ((ShareImageLittleItem) convertView).setItemData(data);
        holder.icon.setImageDrawable(data.getmIcon());
        holder.label.setText(data.getmLabel());
        return convertView;
    }


    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        if (mData == null || mData.size() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        // TODO Auto-generated method stub
        return super.isEnabled(position);
    }

    public void setData(List<ShareImageItemData> data) {
        this.mData = data;
    }
}
